package com.smrs.asn.webapp.action;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.json.annotations.JSON;

import com.jof.framework.util.Pager;
import com.smrs.asn.model.AsnDetailModel;
import com.smrs.asn.model.AsnHeaderModel;
import com.smrs.enums.StatusEnum;
import com.smrs.goods.model.GoodsSkuModel;
import com.smrs.security.model.UserModel;
import com.smrs.util.AppUtil;



public class AsnDetailAction extends BaseAsnAction{
	private static final long serialVersionUID = 1L;
	protected AsnDetailModel asnDetail ;
	protected AsnHeaderModel asnHeader ;
	protected GoodsSkuModel goodsSku;
	protected Pager<AsnDetailModel> pager = new Pager<AsnDetailModel>(); 
	protected String titleName="入库详细";

	
	public GoodsSkuModel getGoodsSku() {
		return goodsSku;
	}


	public void setGoodsSku(GoodsSkuModel goodsSku) {
		this.goodsSku = goodsSku;
	}


	public AsnHeaderModel getAsnHeader() {
		return asnHeader;
	}


	public void setAsnHeader(AsnHeaderModel asnHeader) {
		this.asnHeader = asnHeader;
	}

	public String addAsnDetail(){		
		asnDetail = new AsnDetailModel();
		Pager<AsnDetailModel> testPager = asnDetailService.findBySkuAndAsnHeader(goodsSku, asnHeader,pager);
		goodsSku = goodsSkuService.getByPK(goodsSku.getId());
		//goodsSku = goodsSkuService.getByPK(goodsSku.getId());
		asnHeader = asnHeaderService.getByPK(asnHeader.getId());
		List<AsnDetailModel> list = testPager.getRecords();
		if(CollectionUtils.isNotEmpty(list)){
			this.addActionError(" sku:"+goodsSku.getName()+"  已经在入库单:"+ asnHeader.getSeqNo() +"存在");
			return SUCCESS;
		}
		asnDetail.setAsnId(asnHeader.getId());
		asnDetail.setAsnSeqNo(asnHeader.getSeqNo());
		asnDetail.setAsnId(asnHeader.getId());
		asnDetail.setItemCode(goodsSku.getItemCode());
		asnDetail.setSkuCode(goodsSku.getCode());
		asnDetail.setSkuId(goodsSku.getId());
		asnDetail.setSkuName(goodsSku.getName());		
		asnDetail.setStatus(StatusEnum.ACTIVE.getStatus());
		asnDetail.setActualPurchasePrice(goodsSku.getPurchasePrice());
		asnDetail.setActualQuantify(1d);
		asnDetail.setActualAmount(goodsSku.getPurchasePrice());
		asnDetail.setSeq(asnSequenceService.getAsnDetailNextValue());
		UserModel userModel = this.getUserModelFromSession();
		AppUtil.setCreateUserInfo(userModel, asnDetail);
		
		this.asnDetailService.addModel(asnDetail);	
		
		AsnHeaderModel header = new AsnHeaderModel();
		header.setId(asnDetail.getAsnId());
		asnHeaderService.updateCalculateAsnHeaderQuantifyAndAmount(header);

		
		this.addActionMessage("添加成功");
		return SUCCESS;
	}
	
	
	public String searchAsnDetail(){
		/*AsnDetailSearchModel asnDetailSearchModel = new AsnDetailSearchModel();
		asnDetailSearchModel.setAsnDetail(asnDetail);		
		asnDetailSearchModel.setPager(pager);		
		pager=asnDetailService.getByNameLikePager(asnDetailSearchModel);
		*/
		
		pager = asnDetailService.getByNameLikePager(null, pager);
		return SUCCESS;
	}
	public String addAsnDetailInit(){

		return SUCCESS;
	}
	
	public String updateAsnDetail(){
		
		if(StringUtils.isEmpty(this.actionCommand)){
			asnDetail = asnDetailService.getByPK(asnDetail.getId());	
		}else{
			UserModel userModel = this.getUserModelFromSession();
			AppUtil.setUpdateUserInfo(userModel, asnDetail);
			asnDetailService.updateModel(asnDetail);
			this.addActionMessage("修改成功");
		}
		return SUCCESS;
	}
	
	public String deleteAsnDetail(){
		asnDetailService.deleteModel(asnDetail);
		return SUCCESS;
	}
	
	
	public AsnDetailModel getAsnDetail() {
		return asnDetail;
	}

	public void setAsnDetail(AsnDetailModel asnDetail) {
		this.asnDetail = asnDetail;
	}
	
	@JSON
	public Pager<AsnDetailModel> getPager() {
		return pager;
	}

	
	public void setPager(Pager<AsnDetailModel> pager) {
		this.pager = pager;
	}

	public String getTitleName() {
		return titleName;
	}


	public void setTitleName(String titleName) {
		this.titleName = titleName;
	}
}
